import Mock from 'mockjs'
import { context, isMock } from '@/utils/config'
import qs from 'qs'

;(() => {
  if(!isMock)
    return

  // 登录
  Mock.mock(context + '/member/login', 'post', {
    'errno': 0,
    'data': function() {
      switch (this.errno) {
        case 0:
          return Mock.mock('@guid')
        case 1:
          return
      }
    }
  })

  // 用户验证
  Mock.mock(context + '/member/userCertificate', 'post', {
    'errno': function() {
      /* 模拟登录超时等
      let rand = Math.random()
      if(rand > 0.4) {
        return 0;
      }else if(rand > 0.2 && rand < 0.4) {
        return 8;
      }else {
        return 9;
      }
      */
      return 0;
    },
    'message': function() {
      switch (this.errno) {
        case 8:
          return '用户已过期, 请重新登录!'
        case 9:
          return '登录无效'
      }
    }
  })

  // 用户url
  let reg = context + '/member/?'
  reg = reg.replace(/\//g, '\\/').replace(/^h/, '/h').replace(/\./g, '\\.').replace(/.$/g, '?.*(?!\\/)/')
  reg = eval(reg)

  // 获取用户信息
  Mock.mock(reg, 'get', {
    'errno': 0,
    'data': {
      'id': '@id',
      'nickname': '@name',
      'sex|0-2': 0,
      'imgAvatar': '@image',
      'turename': function() {
        return this.nickname
      },
      'hidePhone': /^1[34578]\d{5}\*\*\*\*$/,
      'phone': /^1[34578]\d{9}$/,
      'email|1': ['', '@email']
    }
  })

  // 用户退出
  Mock.mock(reg, 'delete', {
    'errno': 0,
    'message': 'ok'
  })

  // 短信接口
  Mock.mock(context + '/common/sms', 'post', options => {
    let obj = qs.parse(options.body)
    let errno = isFinite(obj.phone) === true ? 0 : 400 // 简单的判断了
    let message = 'ok'
    if(errno == 400)
      message = '参数异常'
    return {
      errno,
      message
    }
  })

  // 验证短信
  Mock.mock(context + '/common/checkVerificationCode', 'post', options => {
    let obj = qs.parse(options.body)
    let errno = obj.verificationCode === '123456' ? 0 : 6
    let message = 'ok'
    let data = Mock.mock('@guid')
    if(errno === 6) {
      message = '验证码错误'
      data = ''
    }
    return {
      errno,
      message,
      data
    }
  })

  // 房屋列表url
  reg = context + '/info/homelist?'
  reg = reg.replace(/\//g, '\\/').replace(/^h/, '/h').replace(/\./g, '\\.').replace(/.$/g, '?.*(?!\\/)/')
  reg = eval(reg)
  // 房屋列表
  Mock.mock(reg, 'get', {
    errno: 0,
    data: {
      hasMore: function() {
        return Math.random() < 0.8
      },
      data: function() {
        let array = []
        let length = 5
        if(!this.hasMore)
          length = Math.round(Math.random() * 5)

        let pictureArray = [
          'http://ww2.sinaimg.cn/large/0060lm7Tly1flz03w1x49j309m09qgma.jpg', 'http://ww2.sinaimg.cn/large/0060lm7Tly1flz03szt7jj30al0agdik.jpg', 'http://ww4.sinaimg.cn/large/0060lm7Tly1flz03st255j30aj081jv7.jpg', 'http://ww1.sinaimg.cn/large/0060lm7Tly1flz03snhc5j30ar09pwhh.jpg', 'http://ww3.sinaimg.cn/large/0060lm7Tly1flz03sdp2bj306u06q3z2.jpg', 'http://ww3.sinaimg.cn/large/0060lm7Tly1flz03s9rutj305v06zdh5.jpg', 'http://ww1.sinaimg.cn/large/0060lm7Tly1flz06ipj9bj31hc0u0e82.jpg', 'http://ww1.sinaimg.cn/large/0060lm7Tly1flz04pckn1j30zk0jyk1j.jpg', 'http://ww2.sinaimg.cn/large/0060lm7Tly1flz07w28ylj304v04xwfz.jpg']
        for(let i = 0; i < length; i++)
          array.push(Mock.mock({
            'id': '@integer',
            'title|1': ['异世界最豪华的别墅', '全宇宙最大的房子', '新藏海景房一套', '国际空间站'],
            'price': Mock.Random.integer(100, 10000),
            'priceWayContent|1': ['日', '月', '年'],
            'rentWay': Mock.Random.integer(1, 4),
            'rentWayContent': '整租',
            'area': Mock.Random.natural(10, 1000),
            'category': Mock.Random.integer(1, 4),
            'categoryContent': '住宅',
            'decoration': Mock.Random.integer(1, 4),
            'decorationContent': '毛柸',
            'effectiveTime': 1511621124000,
            'roomType': Mock.Random.integer(1, 4),
            'roomTypeContent|1': ['一室', '一室一厅', '两室', '两室一厅', '两室以上'],
            'collectionNum': Mock.Random.integer(1, 1000),
            'state': 0,
            'detailAddress': Mock.Random.integer(1, 4),
            'imgPreview|1': pictureArray,
            'landlordId': Mock.Random.integer(1, 4),
            'nickname': '@name',
            'introduction': '@name',
            'imgAvatar': null,
            'star': Mock.Random.integer(1, 10000)
          }))
        return array
      }
    }
  })
})()
